<template>
  <el-dialog
    :title="!dataForm.recordId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="榜单成员id" prop="memberId">
      <el-input v-model="dataForm.memberId" placeholder="榜单成员id"></el-input>
    </el-form-item>
    <el-form-item label="点赞人id" prop="createUserId">
      <el-input v-model="dataForm.createUserId" placeholder="点赞人id"></el-input>
    </el-form-item>
    <el-form-item label="被点赞的对象的id" prop="toId">
      <el-input v-model="dataForm.toId" placeholder="被点赞的对象的id"></el-input>
    </el-form-item>
    <el-form-item label="点赞来源（被点赞的对象类型）:1里程碑  2评论" prop="source">
      <el-input v-model="dataForm.source" placeholder="点赞来源（被点赞的对象类型）:1里程碑  2评论"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          recordId: 0,
          memberId: '',
          createUserId: '',
          toId: '',
          source: '',
          createTime: ''
        },
        dataRule: {
          memberId: [
            { required: true, message: '榜单成员id不能为空', trigger: 'blur' }
          ],
          createUserId: [
            { required: true, message: '点赞人id不能为空', trigger: 'blur' }
          ],
          toId: [
            { required: true, message: '被点赞的对象的id不能为空', trigger: 'blur' }
          ],
          source: [
            { required: true, message: '点赞来源（被点赞的对象类型）:1里程碑  2评论不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.recordId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.recordId) {
            this.$http({
              url: this.$http.adornUrl(`/rank/likerecord/info/${this.dataForm.recordId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.memberId = data.likeRecord.memberId
                this.dataForm.createUserId = data.likeRecord.createUserId
                this.dataForm.toId = data.likeRecord.toId
                this.dataForm.source = data.likeRecord.source
                this.dataForm.createTime = data.likeRecord.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/rank/likerecord/${!this.dataForm.recordId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'recordId': this.dataForm.recordId || undefined,
                'memberId': this.dataForm.memberId,
                'createUserId': this.dataForm.createUserId,
                'toId': this.dataForm.toId,
                'source': this.dataForm.source,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
